package org.tio.sitexxx.im.server.handler;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.sitexxx.im.common.Command;
import org.tio.sitexxx.im.common.CommandHandler;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.bs.UnSendMsgNtf;
import org.tio.sitexxx.im.common.bs.UnSendMsgReq;
import org.tio.sitexxx.im.server.Ims;
import org.tio.sitexxx.im.server.utils.ImUtils;
import org.tio.sitexxx.service.model.main.ChatRecord;
import org.tio.sitexxx.service.model.main.ChatRecordP2p;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.service.base.UserService;
import org.tio.sitexxx.service.service.stat.ChatRecordP2PService;
import org.tio.sitexxx.service.service.stat.ChatRecordService;
import org.tio.sitexxx.service.vo.SimpleUser;
import org.tio.utils.SystemTimer;
import org.tio.utils.json.Json;

import cn.hutool.core.util.StrUtil;

/**
 * 撤回消息
 * @author tanyaowu
 */
@CommandHandler(Command.UNSEND_MSG_REQ)
public class UnSendMsgReqHandler implements ImServerHandler {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(UnSendMsgReqHandler.class);

	public UnSendMsgReqHandler() {
	}

	@Override
	public void handler(ImPacket packet, ChannelContext channelContext, boolean isWebsocket) throws Exception {
		//		ImSessionContext imSessionContext = ImUtils.getImSessionContext(channelContext);
		UnSendMsgReq unSendMsgReq = Json.toBean(packet.getBodyStr(), UnSendMsgReq.class);

		String mid = unSendMsgReq.getMid();
		String groupid = unSendMsgReq.getG();

		Integer uid = null; //消息发送人
		Long time = null; //消息发送时间

		ChatRecord chatRecord = null;
		ChatRecordP2p chatRecordP2p = null;

		boolean isGroupChat = StrUtil.isNotBlank(groupid);

		if (isGroupChat) { //群聊
			chatRecord = ChatRecordService.me.getByMid(mid);
			if (chatRecord == null) {
				return;
			}
			uid = chatRecord.getUid();
			time = chatRecord.getTime().getTime();
		} else {
			chatRecordP2p = ChatRecordP2PService.me.getByMid(mid);
			if (chatRecordP2p == null) {
				return;
			}
			uid = chatRecordP2p.getUid();
			time = chatRecordP2p.getTime().getTime();
		}

		User user = UserService.ME.getById(uid);
		User curr = ImUtils.getUser(channelContext);
		if (curr == null) {
			ImUtils.info(channelContext, "小小游客，别闹别闹", null);
			return;
		}

		boolean isSelf = Objects.equals(uid, curr.getId());

		//		boolean canUnsend = false;//能否做撤回动作
		if (UserService.isSupper(curr)) {
			//canUnsend = true;//超管有一切权限
		} else {
			if (isSelf) { //是自己发的消息
				if (SystemTimer.currTime - time > 1000 * 120) {
					ImUtils.info(channelContext, "消息已超过两分钟，自己不能撤回，请联系管理员撤回", null);
					return;
				}
			} else {
				ImUtils.info(channelContext, "小伙子，还想撤别人家的消息", null);
				return;
			}
		}

		byte status = isSelf ? ChatRecord.Status.UNDO_BY_SELF : ChatRecord.Status.UNDO_BY_OTHER;

		SimpleUser currSimple = SimpleUser.fromUser(curr);
		SimpleUser userSimple = SimpleUser.fromUser(user);

		UnSendMsgNtf unSendMsgNtf = new UnSendMsgNtf();
		unSendMsgNtf.setMid(mid);
		unSendMsgNtf.setG(groupid);
		unSendMsgNtf.setOper(currSimple);
		unSendMsgNtf.setBeOper(userSimple);
		ImPacket imPacket = new ImPacket(Command.UNSEND_MSG_NTY, unSendMsgNtf);

		if (isGroupChat) { //群聊
			ChatRecordService.me.updateStatus(mid, status);
			ChatRecordService.me.clearLastGroupChat(groupid);

			Ims.sendToGroup(groupid, imPacket);
		} else {
			ChatRecordP2PService.me.updateStatus(mid, status);
			ChatRecordP2PService.me.clearP2pChatCache(chatRecordP2p.getUid(), chatRecordP2p.getTouid());

			Ims.sendToUser(chatRecordP2p.getUid(), imPacket);
			Ims.sendToUser(chatRecordP2p.getTouid(), imPacket);
		}

	}
}
